package com.wangling.service;

import com.wangling.dao.ArticleDao;
import com.wangling.model.Article;
import com.wangling.model.User;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ArticleService {
    private final ArticleDao articleDao = new ArticleDao();
    public Map<String, Object> getIndexData(User user) {
        List<Article> articleList = articleDao.selectArticleListByUid(user.uid);
        int articleCount = articleDao.selectArticleCountByUid(user.uid);
        System.out.println("DEBUG: 文章总数：" + articleCount);

        int typeCount = articleDao.selectTypeCountByUid(user.uid);
        System.out.println("DEBUG: 类型数是：" + typeCount);


        HashMap<String, Object> indexData = new HashMap<>();
        indexData.put("articleList", articleList);
        indexData.put("articleCount", articleCount);
        indexData.put("typeCount", typeCount);
        return indexData;
    }

    public Map<String, Object> getArticleData(User user, int aid) {
        Article article = articleDao.selectOneByAid(aid);
        System.out.println("DEBUG: 对应的文章是：" + article);
        if (article == null) {
            return null;
        }

        int articleCount = articleDao.selectArticleCountByUid(user.uid);
        System.out.println("DEBUG: 文章总数：" + articleCount);

        int typeCount = articleDao.selectTypeCountByUid(user.uid);
        System.out.println("DEBUG: 类型数是：" + typeCount);

        HashMap<String, Object> articleData = new HashMap<>();
        articleData.put("article", article);
        articleData.put("articleCount", articleCount);
        articleData.put("typeCount", typeCount);
        return articleData;
    }

    public Article publishArticle(User user, String title, String content) {
        Article article = new Article();
        article.uid = user.uid;
        article.title = title;
        article.type = "博客";
        article.content = content;
        article.publishedAt = LocalDate.now();

        articleDao.insert(article);

        return article;
    }
}
